<?php
namespace Home\Model;
//物品基础属性
class Goods extends UserBase{
    protected $redis;
    protected $user;
    protected $resource ;
    protected $capacity ;
    protected $component ;
    protected $info;
    protected $quality;
    protected $ready;
    protected $process;
    
    public function __construct($user, $name){
        //数据集
        $pre = getPre($name);
        parent::__construct($user, 'goods');
        $this->name = $name; //描述名//加载
        $info = $this->hMGet(array($pre.'level', $pre.'process'));
        $level = $info[$pre.'level'];
        $process = $info[$pre.'process'];
        $this->level = $level;
        $this->process = $process;
        if(is_numeric($level) && $level > -1){
            $this->ready = true;
        }else{
            $this->ready = false;
        }
    }

    public function getCfg(){
        $name = $this->name;
        $level = $this->level;
        $config = cfg('goods');
        $this->resource = $config[$name]['resource'][$level];//资源
        $this->component = $config[$name]['component'][$level];//组件
        $this->capacity = $config[$name]['capacity'];//所需能力 技能点 ，用来计算制造所花费时间的
        $this->quality = $config[$name]['quality'][$level];//品质
    }
    //解锁，初始化数据
    public function initData(){
        $this->hSetNx($this->name.':level', 0);
    }

    //开始制造，消耗材料，计算时间
    public function start($slot, $base_ts, &$max_quality = null){
        if(!$this->ready) return error('不能制造没有解锁的物品'.$this->name);
        $this->getCfg();
        $component = $this->component;
        $resource = $this->resource;
        $capacity = $this->capacity;
        $s = new \Home\Model\Slot($this->user);
        //检查插槽是否可用
        $oldslot = $s->getFreeSlot('goods', $slot);
        if(!$oldslot) return error('不可用插槽');
        
        //检查技能 以及 计算所需时间
        $ts = $this->checkCapacity($capacity);
        if(!is_numeric($ts)) return error('非法时间'.$ts);
        //检查资源
        $warehouse = new \Home\Model\Warehouse($this->user);
        $bucket = new \Home\Model\Bucket($this->user);
        foreach($resource as $res){
            if(!$bucket->dec($res[0], $res[1], $res[2], true)) return error('资源不足'.$res[0]. $res[1]. $res[2]);
        }
        foreach($component as $com){
            if($com[1]==''){
                //没有等级表示是任务品
                if(!$warehouse->dec($com[0], $com[1], $com[2], true)) return error('任务物品不足'.$com[0]. $com[1]. $com[2]);
            }else{
                //有等级是物品, 检查最高等级限制
                $max_quality = $max_quality || $com[2];
                if(!$warehouse->decAll($com[0], $com[1], $com[2], $max_quality, true)) return error('当前品质组件不足');
            }
        }
        //减资源
        foreach($resource as $res){
            if($bucket->dec($res[0], $res[1], $res[2]) === false) return error('资源不足'.$res[0]. $res[1]. $res[2]);
        }
        foreach($component as $com){
            if($com[1]==''){
                //没有等级表示是任务品
                if($warehouse->dec($com[0], $com[1], $com[2]) === false) return error('任务物品不足'.$com[0]. $com[1]. $com[2]);
            }else{
                //有等级是物品
                if($warehouse->decAll($com[0], $com[1], $com[2], $max_quality) === false) return error('当前品质组件不足');
            }
        }
        $s->setSlot('goods', $slot, $base_ts, $ts, $this->name, $this->quality);
        return true;
    }

    public function checkCapacity($capacity){
        $workers = new \Home\Model\Worker($this->user);
        
        $ts = 0;
        foreach($capacity as $key=>$value){
            //如果某项技能为空,就报错
            $myCap = $workers->getCapacity($key);
            if(!$myCap) return error('没有会该项技能的员工'.$key);
            $ts += $value / $myCap;
        }
        $ts = ceil($ts);
        return $ts;
    }

    public function process(){
        $name = $this->name;
        $level = $this->level;
        $config = cfg('goods', $name);
        $unlock = $config['unlock'][$level];//如果升级解锁什么，
        $unlock_amount = $config['unlock_amount'][$level];//解锁需要的数量
        $process = $this->hIncrBy($name.':process', 1);
        // error($level.'`'.$process.'`'. $unlock_amount);
        if($process == $unlock_amount && $level <= 4){
            //可以升级了
            $this->hSet($name.':level', $level + 1);
            //解锁名称中不含空格,其他动作有空格
            if(strpos($unlock, '[') === false) return true;
            $unlock = str_replace('[','', $unlock);
            $unlock = str_replace(']','', $unlock);
            $newgoods = getClass('goods', $this->user, $unlock);
            //初始化一个物品,设置等级为0 
            $newgoods->initData();
        }
    }


} 